<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Queue\Capsule\Manager;
use Session;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\CreateBankShopEquRequest;
use App\Http\Requests\UpdateBankShopEquRequest;
use App\BankShop;
use App\BankShop_equipment;
use Illuminate\Support\Facades\Auth;

class EquipmentController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

	public function index(Request $request) {
		// 查看当前菜单可使用的功能按钮
		$uri = $request->path();
		$data['optionArr'] =  $this->checkMenuOption($uri);


		/**
		 *  判断当前登录账号，如果是第三方网点公司账号，则查询显示当前网点的数据
		 *  如果不是，则默认显示所有
		 */
		$adminUser = Auth::user();
		$phone = $adminUser->name;
		$bank_id = BankShop::getIdByPhone($phone);
		if($bank_id){
			$bankShopEp = BankShop_equipment::getTheOneEquipmentBankShop($bank_id);
		}else{
			$bankShopEp = BankShop_equipment::getEquipmentBankShop();
		}
//			dd($bankShopEp);

		$data['bankShopEp'] = $bankShopEp;

		return view('bankShopEp.list')->with($data);
	}

    public function create(Request $request)
    {
    	// 获取所有网点
	    $bankShop = BankShop::getAllBankShopId();
//	    dd($bankShop);
	    $data['bankShop'] = $bankShop;
        return view('bankShopEp.create')->with($data);
    }


    public function store(CreateBankShopEquRequest $request){
        DB::beginTransaction();
        $bankShopEP = new BankShop_equipment();
	    $bankShopEP->e_code =$request->e_code;
	    $bankShopEP->e_name =$request->e_name;
	    $bankShopEP->bank_show_id =$request->bank_show_id;
	    $bankShopEP->e_dec =$request->e_dec;
	    $bankShopEP->state =1; // 默认启用状态
	    $bankShopEP->contact =$request->contact;
	    $bankShopEP->phone =$request->phone;
	    $bankShopEP->device_id =$request->device_id;

        if($bankShopEP->save()){
            DB::commit();

	        $adminUser = Auth::user();
	        if($adminUser){
		        $userId = $adminUser->id;
		        $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').' 新增了【  '.$request->e_name.' 】设备';
		        adminOperationLog($request,$userId,$logs);
	        }

            return redirect('equipment/list')->with('success','新增成功');
        }else{
            DB::rollback();
            return redirect()->back()->withInput();
        }
    }

	public function showOrder($id)
	{
		// 获取设备下的订单
		$shopOrderObj = BankShop_equipment::getShopOrderS($id);
//		dd($shopOrderObj);

		$data['shopOrderInfo'] = $shopOrderObj;

		return view('bankShopEp.showOrder')->with($data);

	}

    public function edit($id)
    {
	    $bankShopEp = BankShop_equipment::find($id);
	    $bankShop = BankShop::getAllBankShopId();
        $data['bankShopEpInfo'] = $bankShopEp;
        $data['bankShop'] = $bankShop;
//        dd($bankShop);

        return view('bankShopEp.edit')->with($data);

    }


    public function update(UpdateBankShopEquRequest $request, $id){

        DB::beginTransaction();
	    $bankShopEP = BankShop_equipment::find($id);
	    $bankShopEP->e_code =$request->e_code;
	    $bankShopEP->e_name =$request->e_name;
	    $bankShopEP->bank_show_id =$request->bank_show_id;
	    $bankShopEP->e_dec =$request->e_dec;
	    $bankShopEP->contact =$request->contact;
	    $bankShopEP->phone =$request->phone;
	    $bankShopEP->device_id =$request->device_id;

        if(!$bankShopEP->save()){
            DB::rollback();
            return Redirect::back()->withInput()->withErrors(['fail' => trans('message.updateFail')]);
        }

        DB::commit();

	    $adminUser = Auth::user();
	    if($adminUser){
		    $userId = $adminUser->id;
		    $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'编辑了【 '.$request->name.' 】设备';
		    adminOperationLog($request,$userId,$logs);
	    }

        return Redirect::to(url('equipment/list'))->with('success', trans('message.updateSuccess'));

    }

	// 修改设备状态
	public function changShopState(Request $request){
		$id = $request->id;
		$state = $request->state;

		if(!$id || !$state){
			return -1;
		}

		$bankShopEP = BankShop_equipment::find($id);
		// 如果停用，检查该设备是否有未完成的订单，如果有未完成的订单，则不能停用
		if($state == 2){
			if(count(DB::table('order')->where('e_code',$bankShopEP->e_code)->where('state','<=',3)->get())){
				return '-3';
			}
		}

		if($bankShopEP){
			$bankShopEP->state = $state;
			$bankShopEP->save();

			$name = BankShop_equipment::getNameById($id);
			$adminUser = Auth::user();
			if($adminUser){
				$userId = $adminUser->id;
				$logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'修改了【 '.$name.' 】设备状态';
				adminOperationLog($request,$userId,$logs);
			}

			return 1;
		};
		return -1;
	}

    public function delete(Request $request){
		$eid = $request->id;
        if(!$eid){
            return '-1';
        }
	    $name = BankShop_equipment::getNameById($eid);

        DB::beginTransaction();
        //  del
	    $affectId= BankShop_equipment::where('id',$eid)->update([
		    'state'=>-1
	    ]);
//        $affectId = BankShop_equipment::where('id',$request->id)->delete();
        if(!$affectId){
            DB::rollback();
            return -1;
        }

        DB::commit();

	    $adminUser = Auth::user();
	    if($adminUser){
		    $userId = $adminUser->id;
		    $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'删除了设备：【 '.$name.' 】';
		    adminOperationLog($request,$userId,$logs);
	    }

        return 1;

    }


}





